﻿using System;
using System.Data;
using WorkOrder.Business;

namespace WorkOrder.workBill
{
    public partial class billAvgTime : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserID"].ToString() == "")
            {
                Session.Clear();
                ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
            }
        }


        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            if (txtStartC.Value.Trim() == "" && txtEndC.Value.Trim() == "" && txtStartF.Value.Trim()=="" && txtEndF.Value.Trim()=="")
                for (int i = 1; i <= 6; i++)
                {
                    tab1.Rows[i].Cells[1].InnerText = "0";
                    tab1.Rows[i].Cells[2].InnerText = "0";
                    tab1.Rows[i].Cells[3].InnerText = "0";
                    tab2.Rows[i].Cells[1].InnerText = "0";
                    tab2.Rows[i].Cells[2].InnerText = "0";
                    tab2.Rows[i].Cells[3].InnerText = "0";
                }
            FillData();
        }

        private void FillData()
        {
            DataTable tmp1 = null, tmp2 = null, tmp3 = null, tmp4 = null, tmp5 = null, tmp6 = null;
            BillAvgTime bat=new BillAvgTime();
            if (txtStartC.Value.Trim() != "" && txtEndC.Value.Trim() != "" && (txtStartF.Value.Trim() == "" || txtEndF.Value.Trim() == ""))
            {
                tmp1 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), "", "", 1, "CREATE");
                tmp2 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), "", "", 3, "CREATE");
                tmp3 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), "", "", 2, "CREATE");
                tmp4 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), "", "", 1, "CRM");
                tmp5 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), "", "", 3, "CRM");
                tmp6 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), "", "", 2, "CRM");
            }
            else if ((txtStartC.Value.Trim() == "" || txtEndC.Value.Trim() == "") && txtStartF.Value.Trim() != "" && txtEndF.Value.Trim() != "")
            {
                tmp1 = bat.GetAvgTime("", "", txtStartF.Value.Trim(), txtEndF.Value.Trim(), 1, "CREATE");
                tmp2 = bat.GetAvgTime("", "", txtStartF.Value.Trim(), txtEndF.Value.Trim(), 3, "CREATE");
                tmp3 = bat.GetAvgTime("", "", txtStartF.Value.Trim(), txtEndF.Value.Trim(), 2, "CREATE");
                tmp4 = bat.GetAvgTime("", "", txtStartF.Value.Trim(), txtEndF.Value.Trim(), 1, "CRM");
                tmp5 = bat.GetAvgTime("", "", txtStartF.Value.Trim(), txtEndF.Value.Trim(), 3, "CRM");
                tmp6 = bat.GetAvgTime("", "", txtStartF.Value.Trim(), txtEndF.Value.Trim(), 2, "CRM");
            }
            else if (txtStartC.Value.Trim() != "" && txtEndC.Value.Trim() != "" && txtStartF.Value.Trim() != "" && txtEndF.Value.Trim() != "")
            {
                tmp1 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), txtStartF.Value.Trim(), txtEndF.Value.Trim(), 1, "CREATE");
                tmp2 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), txtStartF.Value.Trim(), txtEndF.Value.Trim(), 3, "CREATE");
                tmp3 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), txtStartF.Value.Trim(), txtEndF.Value.Trim(), 2, "CREATE");
                tmp4 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), txtStartF.Value.Trim(), txtEndF.Value.Trim(), 1, "CRM");
                tmp5 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), txtStartF.Value.Trim(), txtEndF.Value.Trim(), 3, "CRM");
                tmp6 = bat.GetAvgTime(txtStartC.Value.Trim(), txtEndC.Value.Trim(), txtStartF.Value.Trim(), txtEndF.Value.Trim(), 2, "CRM");
            }
            #region 平均历时

            for (int i = 1; i <= 6; i++)
            {
                #region 从派单时间开始计算

                #region 先将所有单元格填0

                tab1.Rows[i].Cells[1].InnerText = "0";
                tab1.Rows[i].Cells[2].InnerText = "0";
                tab1.Rows[i].Cells[3].InnerText = "0";

                #endregion

                #region 填充数据
                if (tmp1 != null)
                    for (int j = 0; j < tmp1.Rows.Count; j++)
                    {
                        if (tab1.Rows[i].Cells[0].InnerText.Trim().Replace("\r\n", "").Trim() ==
                            tmp1.Rows[j][0].ToString())
                        {
                            tab1.Rows[i].Cells[1].InnerHtml = tmp1.Rows[j][1].ToString();
                        }
                    }
                if (tmp2 != null)
                    for (int j = 0; j < tmp2.Rows.Count; j++)
                    {
                        if (tab1.Rows[i].Cells[0].InnerText.Trim().Replace("\r\n", "").Trim() ==
                            tmp2.Rows[j][0].ToString())
                        {
                            tab1.Rows[i].Cells[2].InnerHtml = tmp2.Rows[j][1].ToString();
                        }
                    }
                if (tmp3 != null)
                    for (int j = 0; j < tmp3.Rows.Count; j++)
                    {
                        if (tab1.Rows[i].Cells[0].InnerText.Trim().Replace("\r\n", "").Trim() ==
                            tmp3.Rows[j][0].ToString())
                        {
                            tab1.Rows[i].Cells[3].InnerHtml = tmp3.Rows[j][1].ToString();
                        }
                    }

                #endregion

                #endregion


                #region 从CRM资源确认时间开始计算

                #region 先将所有单元格填0

                tab2.Rows[i].Cells[1].InnerText = "0";
                tab2.Rows[i].Cells[2].InnerText = "0";
                tab2.Rows[i].Cells[3].InnerText = "0";

                #endregion

                #region 填充数据

                if (tmp4 != null)
                    for (int j = 0; j < tmp4.Rows.Count; j++)
                    {
                        if (tab2.Rows[i].Cells[0].InnerText.Trim().Replace("\r\n", "").Trim() ==
                            tmp4.Rows[j][0].ToString())
                        {
                            tab2.Rows[i].Cells[1].InnerHtml = tmp4.Rows[j][1].ToString();
                        }
                    }
                if (tmp5 != null)
                    for (int j = 0; j < tmp5.Rows.Count; j++)
                    {
                        if (tab2.Rows[i].Cells[0].InnerText.Trim().Replace("\r\n", "").Trim() ==
                            tmp5.Rows[j][0].ToString())
                        {
                            tab2.Rows[i].Cells[2].InnerHtml = tmp5.Rows[j][1].ToString();
                        }
                    }
                if (tmp6 != null)
                    for (int j = 0; j < tmp6.Rows.Count; j++)
                    {
                        if (tab2.Rows[i].Cells[0].InnerText.Trim().Replace("\r\n", "").Trim() ==
                            tmp6.Rows[j][0].ToString())
                        {
                            tab2.Rows[i].Cells[3].InnerHtml = tmp6.Rows[j][1].ToString();
                        }
                    }

                #endregion

                #endregion
            }


            #endregion
        }
    }
}